$(function() {
	
	jQuery(document).ready(function () {
		getGridInfo();
	});
	
	function getGridInfo(){
		var id = jQuery('#gridId').val();
		 var url=window.rootPath+"/ui/index/getGridInfo/"+id;
         jQuery.ajax({
             url:encodeURI(url),
             cache:false,
             type : "get",
             dataType:"json",
             success:function(data){
            	 	//网格基本信息
            	 	var grid = data.grid;
            	 	if(grid){
	         	 	jQuery('#gridIdentify').val(grid.gridIdentify);
	         	 	jQuery('#gridName').val(grid.gridName);
	         	 	jQuery('#gridArea').val(grid.gridArea+'㎡');
	         	 	jQuery('#gridScale').val(grid.gridScale);
	         	 	jQuery('#gridState').val(grid.gridState.title);
	         	 	jQuery('#typeOpt').val(grid.typeOpt.title);
	         	 	if(grid.division){
	         	 		jQuery('#division').val(grid.division.divisionName);	
	         	 	}
	         	 	jQuery('#gridMemo').text(grid.gridMemo);
             	}
            	 	
            	 	//塔型结构
            	 	var gradings = data.gradings;
    				if(gradings.length > 0){
        				for(var i=0;i<gradings.length;i++){
                			var item = {};
                    		item.name = gradings[i].gradingName;
                    		item.value = gradings[i].gradingLevel.extended1;
                    		item.id = gradings[i].id;
                    		funnelData.push(item);
        				}
    				}else{
    					funnelData = [];
    				}
    				bubbleSort(funnelData);
    				var dataTemp = reSetSortNum(funnelData);
    				updateFunnel(dataTemp);
         	 	
    				//网格管理人员
    				var gridUsers = data.gridUsers;
    				if(gridUsers && gridUsers.length > 0){
    					var contentHtml = '';
    					var len = gridUsers.length
    					var levelHtml = '';
    					var userHtml = '';
    					var temp = '';
    					for(var i=0;i<len;i++){
    						var detail = gridUsers[i];
    						if(temp != detail.grading.gradingLevel.extended1){
    							contentHtml += levelHtml + userHtml + '</div></div>';
    							levelHtml = '';
    							userHtml = '';
    							temp = detail.grading.gradingLevel.extended1
    						}
    						if(detail.user){
    							userHtml += '<label class="col-sm-4 text-center">'+detail.user.name+'</label>';
    						}else{
    							levelHtml += '<div class="form-group"><label class="col-sm-2 text-right">'+detail.grading.gradingName+'</label><div class="col-sm-10">';
    						}
    					}
    					jQuery('#TheCharts04').empty();
    					jQuery('#TheCharts04').append(contentHtml);
    				}
    				
    				var eventReports = data.eventReports;
    				var eventLen = eventReports.length;
    				if(eventReports && eventLen > 0){
    					var contentHtml = '';
	    				for(var j=0;j<eventLen;j++){
	    					contentHtml += '<tr><td>'+moment(eventReports[j].reportAccTime).format('YYYY/MM/DD')+'</td><td>'+eventReports[j].eventName+'</td><td>'+eventReports[j].eventTaskUserName+'</td></tr>';
	    				}
	    				jQuery('#eventReport').empty();
    					jQuery('#eventReport').append(contentHtml);
    				}
    				var analysisData = data.analysisData;
    				updatePie(analysisData);
             }
         });
	}
	
	//塔型结构
	var funnel = echarts.init(document.getElementById('TheCharts03'));
	var funnelData = [];
    var option = {
    		tooltip: {
    			trigger: 'item',
    			formatter: "{b}"
    		},
        calculable: false,
        series: [{
            name: '塔型结构',
            type: 'funnel',
            left: '10%',
			top: 60,
			bottom: 60,
			width: '60%',
			min: 0,
            max: 1,
			minSize: '0%',
			maxSize: '100%',
			sort: 'ascending',
			gap: 2,
			label: {
				normal: {
					show: true,
					position: 'inside'
				},
				emphasis: {
					textStyle: {
						fontSize: 20
					}
				}
			},
			labelLine: {
				normal: {
					length: 10,
					lineStyle: {
						width: 1,
						type: 'solid'
					}
				}
			},
			itemStyle: {
				normal: {
					borderColor: '#fff',
					borderWidth: 1
				}
			},
			data: []
		}]
    };
    
    funnel.setOption(option);
	
    
    //冒泡排序
    function bubbleSort(arr) {
        var len = arr.length;
        for (var i = 0; i < len; i++) {
            for (var j = 0; j < len - 1 - i; j++) {
                if (arr[j].value > arr[j+1].value) {        //相邻元素两两对比
                    var temp = arr[j+1];        //元素交换
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
    //重新设置排序号
    function reSetSortNum(arr){
    		var newObject = jQuery.extend(true, [], arr);
    		var len = newObject.length;
    		for(var i=0;i<len;i++){
    			newObject[i].value = i+1;
    		}
    		return newObject;
    }
    //更新数据
    function updateFunnel(data){
	    	funnel.setOption({
	    		series:{
	    			max:data.length,
	    			data :data
	    		}
	    	});
    }
    
    
    /**
     * 今日案件
     */
    var pie2;
    pie2 = echarts.init(document.getElementById('TheCharts01'));
    var option2 = {
    		tooltip: {
    			trigger: 'item',
    			formatter: "{b}: {c} ({d}%)"
    		},
//    		color: ['#67e0e3', '#e062ae', '#ffdb5c'],
    		legend: {
    			y: 'bottom',
    			data: [],
    			textStyle: {
    				fontSize: '12',
    				color: '#fff'
    			}
    		},
    		series: [{
    			name: '访问来源',
    			type: 'pie',
    			radius: ['50%', '70%'],
    			avoidLabelOverlap: false,
    			label: {
    				normal: {
    					show: false,
    					position: 'center'
    				},
    				emphasis: {
    					show: true,
    					textStyle: {
    						fontSize: '30',
    						fontWeight: 'bold'
    					}
    				}
    			},
    			labelLine: {
    				normal: {
    					show: false
    				}
    			},
    			data: []
    		}]
    	};
    pie2.setOption(option2);
    
    function updatePie(data){
    		pie2.setOption({
			legend:{ data : data.legend },
			series:{ data : data.series }
		});
    }
});

